package com.exam.controller;


import com.exam.entity.ApiResult;
import com.exam.entity.Score;
import com.exam.serviceimpl.ScoreServiceImpl;
import com.exam.util.ApiResultHandler;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "scorecontroller")
public class ScoreController {
    @Autowired
    private ScoreServiceImpl scoreService;

    @RequestMapping("/scores")
    public ResponseEntity<List<Score>> findAll() {
        List<Score> res = scoreService.findAll();
       return new ResponseEntity<List<Score>>(res, HttpStatus.OK);
    }
//    分页
    @RequestMapping("/score1/{page}/{size}/{studentId}")
    public ResponseEntity<Page<Score>> findById(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("studentId") Integer studentId) {
        Page<Score> scorePage = new Page<>(page, size);
        List<Score> scoreList=this.scoreService.findById(page, size, studentId);
        return  new ResponseEntity<Page<Score>>((Page<Score>) scoreList,HttpStatus.OK);
    }

//    不分页
    @RequestMapping("/score2/{studentId}")
        public ResponseEntity<List<Score>> findById(@PathVariable("studentId") Integer studentId) {
        List<Score> res = scoreService.findById(studentId);
        return new ResponseEntity<List<Score>>(res,HttpStatus.OK);
    }

    @RequestMapping("/addscore")
    public ResponseEntity<Integer> add(@RequestBody Score score) {
        int res = scoreService.add(score);
        return new ResponseEntity<Integer>(res,HttpStatus.OK);
    }

    @RequestMapping("/findByExamCode/{examCode}")
    public ResponseEntity<List<Score>> findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<Score> scores = scoreService.findByExamCode(examCode);
        return  new ResponseEntity<List<Score>>(scores,HttpStatus.OK);
    }
}
